<template>
  <div class="first">
    <el-button type="primary" @click="addSelect">添加品牌</el-button>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="请选择分类">
          <el-select v-model="form.region" :label-width="formLabelWidth">
            <el-option
              :label="item.categoryName"
              :value="item.id"
              v-for="item in selectList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-input v-model="form.name" autocomplete="off" placeholder="请输入产品名称"></el-input>
        <el-form-item>
          <el-upload
            action="http://127.0.0.1:3000/product/addProductPic"
            :with-credentials="true"
            name="pic1"
            list-type="picture-card"
            :on-success="handlesuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt>
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addupload">保 存</el-button>
      </div>
    </el-dialog>
    <el-table
      ref="multipleTable"
      :data="tableData3"
      tooltip-effect="dark"
      style="width: 100%"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <!-- <el-table-column type="index" width="50"></el-table-column> -->
      <el-table-column prop="id" label="品牌编号" width="280" align="center"></el-table-column>
      <el-table-column prop="brandName" label="品牌名称" width="280" align="center"></el-table-column>
      <el-table-column prop="brandLogo" label="品牌logo" width="280" align="center">
        <template slot-scope="scope">
          <img class="photo" :src="'http://127.0.0.1:3000'+scope.row.brandLogo" alt>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="所属分类" width="280"></el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="CurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { SecondCate, firstCate, addCateSecond } from '@/api'
export default {
  created () {
    this.initList()
  },
  data () {
    return {
      // 当前页
      currentPage4: 1,
      total: 100,
      pagesize: 5,
      tableData3: [],
      multipleSelection: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogImageUrl: '',
      dialogVisible: false,
      // 添加图片
      addPicAddr: '',
      // 下拉列表
      selectList: [],
      // 添加框
      form: {
        name: '',
        region: ''
      },
      formLabelWidth: '30px'

    }
  },
  methods: {
    // 初始化页面
    initList () {
      SecondCate({ page: this.currentPage4, pageSize: this.pagesize })
        .then(res => {
          console.log(res)
          this.total = res.data.total
          this.tableData3 = res.data.rows
        })
    },
    // 新增品牌下拉列表
    addSelect () {
      this.dialogFormVisible = true
      firstCate({ page: 1, pageSize: 50 })
        .then(res => {
          console.log(res)
          this.selectList = res.data.rows
        })
    },
    // 新增品牌
    addupload () {
      this.dialogFormVisible = false
      // console.log(this.form.region)
      addCateSecond({ brandName: this.form.name, categoryId: this.form.region, brandLogo: this.addPicAddr })
        .then(res => {
          console.log(res)
          this.initList()
        })
    },
    // 上传图片
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handlesuccess (res) {
      // console.log('-------------------')
      console.log(res)
      console.log(res.picAddr)
      // console.log(file)
      // console.log(fileList)
      // console.log('-------------------')
      this.addPicAddr = res.picAddr
    },
    // 添加框
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => { })
    },
    // 以下表格与分页自带预设
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleDelete (index, row) {
      console.log(index, row)
    },
    setCurrent (row) {
      this.$refs.singleTable.setCurrentRow(row)
    },
    handleCurrentChange (val) {
      this.currentRow = val
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.initList()
    },
    CurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.currentPage4 = val
      this.initList()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-table::before {
  height: 0;
}
td {
  text-align: center;
}
.el-input {
  margin-bottom: 15px;
}
.photo {
  width: 150px;
}
</style>
